package gupao.concurrency.demo.risk.calc.simulator;

import gupao.concurrency.demo.risk.model.NodeTree;
import gupao.concurrency.demo.risk.model.RiskCapital;
import gupao.concurrency.demo.risk.model.Node;

import java.util.List;

public class CalcSimulator {

    //mock calculation qaf ratio for a node
    public static void calcNodeQaf(Node node, NodeTree tree){
        List<Node> children = tree.getChildren(node);

        if(children == null || children.size() == 0){
            TimeConsumeSimulator.consumeTimeToCalcQaf();
            node.setQafRatio(1.0D);
            return;
        }

        double x = 1.0D;
        for (Node child : children) {
            x += child.getQafRatio() * 1.1;
            TimeConsumeSimulator.consumeTimeToCalcQaf();
        }

        node.setQafRatio(x);
    }

    public static void calcRiskCaptital(Node node, NodeTree tree){
        RiskCapital riskCapital = new RiskCapital();
        riskCapital.setExpLoss(1.18);
        riskCapital.setVal(riskCapital.getExpLoss() / 0.618);

        List<Node> children = tree.getChildren(node);
        if(children == null || children.size() == 0){
            TimeConsumeSimulator.consumeTimeToCalcCapital();
            node.setRiskCapital(riskCapital);
            return;
        }

        for (Node c : children) {
            Double el = (riskCapital.getExpLoss() + c.getRiskCapital().getExpLoss()) * 3 / 4;
            Double val = (riskCapital.getVal() + c.getRiskCapital().getVal()) * 4 / 3;
            riskCapital.setVal(val);
            riskCapital.setExpLoss(el);
            TimeConsumeSimulator.consumeTimeToCalcCapital();
        }
        node.setRiskCapital(riskCapital);
    }
}
